﻿using Microsoft.Practices.CompositeWeb;
using ModulesToolkit.DataAccess.Membership;
using ModulesToolkit.DataContracts.Membership;

namespace ModulesToolkit.Membership.Views
{
    public class RolesListPresenter : Presenter<IRolesListView>
    {
        #region Fields

        private readonly RolesRepository _rolesRepository;
        private readonly ScreenRightsDtoRipository _screenRightsDtoRipository;
        private readonly ScreensRipository _screensRipository;

        #endregion Fields

        #region Constructors

        public RolesListPresenter(RolesRepository rolesRepository,
            ScreenRightsDtoRipository screenRightsDtoRepository,
            ScreensRipository screensRipository
            )
        {
            _rolesRepository = rolesRepository;
            _screenRightsDtoRipository = screenRightsDtoRepository;
            _screensRipository = screensRipository;
        }

        #endregion Constructors

        #region Methods

        public bool IsInRights(int rightId)
        {
            return false;
        }

        public void OnAddCommandClik()
        {
            this.View.Role = new Role();
            var screens = _screensRipository.GetScreens(new ScreensSearchCriteria());
            //var screenRights = _screenRightsDtoRipository.RetrieveScreenRights(new ScreenRightDtoSearchCriteria());
            this.View.FillScreensAccordion(screens);
            //this.View.FillScreenRights(screenRights);
            this.View.ShowDetailsPopup();
        }

        public void OnDeleteCommand(int id)
        {
            this._rolesRepository.DeleteRole(id);
            this.OnLoadRolesList();
        }

        public void OnLoadScreenRights()
        {
            var rights = this._screenRightsDtoRipository.RetrieveScreenRights(this.View.ScreenRightSearchCriteria);
            this.View.FillScreenRights(rights);
        }

        public void OnLoadRolesList()
        {
            var entitySearchResult = this._rolesRepository.GetRoles(this.View.SearchCriteria);
            this.View.FillRolesList(entitySearchResult);
        }
        public void OnLoadScreens()
        {
            var screen = this._screensRipository.GetScreens(this.View.ScreensSearchCriteria);
            this.View.FillScreensAccordion(screen);
        }
        //public void OnLoadSreenRights()
        //{
        //    var screenRights = this._screenRightsDtoRipository
        //}
        public void OnSearchClick()
        {
            this.OnLoadRolesList();
        }

        public bool OnValidateRoleNameDuplicate(string name)
        {
            // var result = _rolesRepository.
            return true;
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        #endregion Methods
    }
}